

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>quippy.potential &mdash; quippy 7ea04c5+ documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
        <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"processClass": "math|output_area", "processEscapes": true, "ignoreClass": "document", "inlineMath": [["$", "$"], ["\\(", "\\)"]]}})</script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/theme_overrides.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> quippy
          

          
            
            <img src="../../_static/hybrid.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                7ea04c5+
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
    
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../intro.html">Introduction to <code class="docutils literal notranslate"><span class="pre">quippy</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation of QUIP and quippy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Tutorials/index.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../quippy.html">Quippy library reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../visualisation.html">Visualisation Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fortran_wrapper.html">Appendix: wrapping Fortran 90 Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../teach_sparse.html">Command line options of the teach_sparse main program</a></li>
</ul>

            
          
    <p></p>
    <ul>
        <li><a href="../../genindex.html">Index</a></li>
        <li><a href="../../py-modindex.html">Module Index</a></li>
        <li><a href="../../search.html">Search Page</a></li>
        <li><a href="../index.html">Module Source Code</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">quippy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>quippy.potential</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for quippy.potential</h1><div class="highlight"><pre>
<span></span><span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   quippy: Python interface to QUIP atomistic simulation library</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   Copyright James Kermode 2010</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   These portions of the source code are released under the GNU General</span>
<span class="c1"># HQ X   Public License, version 2, http://www.gnu.org/copyleft/gpl.html</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   If you would like to license the source code under different terms,</span>
<span class="c1"># HQ X   please contact James Kermode, james.kermode@gmail.com</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   When using this software, please cite the following reference:</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   http://www.jrkermode.co.uk/quippy</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">weakref</span>
<span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">sqrt</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="k">import</span> <span class="n">path</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">quippy</span> <span class="k">import</span> <span class="n">_potential</span>
<span class="kn">from</span> <span class="nn">quippy._potential</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">quippy</span> <span class="k">import</span> <span class="n">get_fortran_indexing</span>
<span class="kn">from</span> <span class="nn">quippy.clusters</span> <span class="k">import</span> <span class="n">HYBRID_NO_MARK</span><span class="p">,</span> <span class="n">HYBRID_ACTIVE_MARK</span>
<span class="kn">from</span> <span class="nn">quippy.oo_fortran</span> <span class="k">import</span> <span class="n">update_doc_string</span>
<span class="kn">from</span> <span class="nn">quippy.atoms</span> <span class="k">import</span> <span class="n">Atoms</span>
<span class="kn">from</span> <span class="nn">quippy.util</span> <span class="k">import</span> <span class="n">quip_xml_parameters</span><span class="p">,</span> <span class="n">dict_to_args_str</span>
<span class="kn">from</span> <span class="nn">quippy.elasticity</span> <span class="k">import</span> <span class="n">stress_matrix</span>
<span class="kn">from</span> <span class="nn">quippy.farray</span> <span class="k">import</span> <span class="n">farray</span><span class="p">,</span> <span class="n">frange</span><span class="p">,</span> <span class="n">fzeros</span>

<span class="vm">__doc__</span> <span class="o">=</span> <span class="n">_potential</span><span class="o">.</span><span class="vm">__doc__</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="n">_potential</span><span class="o">.</span><span class="n">__all__</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;force_test&#39;</span><span class="p">,</span> <span class="s1">&#39;Minim&#39;</span><span class="p">,</span> <span class="s1">&#39;ForceMixingPotential&#39;</span><span class="p">]</span>

<span class="n">potlog</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;quippy.potential&#39;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">calculator_callback_factory</span><span class="p">(</span><span class="n">calculator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return a Python function which can be used as a quippy</span>
<span class="sd">       :class:`Potential` callback routine for the given ASE calculator.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">callback</span><span class="p">(</span><span class="n">at</span><span class="p">):</span>
        <span class="n">at</span><span class="o">.</span><span class="n">set_calculator</span><span class="p">(</span><span class="n">calculator</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">at</span><span class="o">.</span><span class="n">calc_energy</span><span class="p">:</span>
            <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">get_potential_energy</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">at</span><span class="o">.</span><span class="n">calc_local_e</span><span class="p">:</span>
            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;local_e&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">at</span><span class="o">.</span><span class="n">local_e</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">get_potential_energies</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">at</span><span class="o">.</span><span class="n">calc_force</span><span class="p">:</span>
            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">at</span><span class="o">.</span><span class="n">force</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">get_forces</span><span class="p">()</span><span class="o">.</span><span class="n">T</span>
        <span class="k">if</span> <span class="n">at</span><span class="o">.</span><span class="n">calc_virial</span><span class="p">:</span>
            <span class="n">stress</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">get_stress</span><span class="p">()</span>
            <span class="n">virial</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
            <span class="n">virial</span><span class="p">[:,:]</span> <span class="o">=</span> <span class="n">stress_matrix</span><span class="p">(</span><span class="o">-</span><span class="n">stress</span><span class="o">*</span><span class="n">at</span><span class="o">.</span><span class="n">get_volume</span><span class="p">())</span>
            <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;virial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">virial</span>
        <span class="k">if</span> <span class="n">at</span><span class="o">.</span><span class="n">calc_local_virial</span><span class="p">:</span>
            <span class="n">stresses</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">get_stresses</span><span class="p">()</span>
            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;local_virial&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">n_cols</span><span class="o">=</span><span class="mi">9</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">lv</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">local_virial</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span>
            <span class="n">vol_per_atom</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">get_volume</span><span class="p">()</span><span class="o">/</span><span class="nb">len</span><span class="p">(</span><span class="n">at</span><span class="p">)</span>
            <span class="n">lv</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">stresses</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">at</span><span class="p">),</span><span class="mi">9</span><span class="p">))</span><span class="o">*</span><span class="n">vol_per_atom</span>

    <span class="k">return</span> <span class="n">callback</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">ase.calculators.calculator</span> <span class="k">import</span> <span class="n">Calculator</span><span class="p">,</span> <span class="n">all_changes</span><span class="p">,</span> <span class="n">all_properties</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="n">potlog</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;Atomic Simulation Environment (ASE) not installed, limited functionality available&#39;</span><span class="p">)</span>
    <span class="n">Calculator</span> <span class="o">=</span> <span class="nb">object</span>
    <span class="n">all_changes</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">all_properties</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="Potential"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential">[docs]</a><span class="k">class</span> <span class="nc">Potential</span><span class="p">(</span><span class="n">_potential</span><span class="o">.</span><span class="n">Potential</span><span class="p">,</span> <span class="n">Calculator</span><span class="p">):</span>
    <span class="vm">__doc__</span> <span class="o">=</span> <span class="n">update_doc_string</span><span class="p">(</span><span class="n">_potential</span><span class="o">.</span><span class="n">Potential</span><span class="o">.</span><span class="vm">__doc__</span><span class="p">,</span> <span class="sa">r</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">The :class:`Potential` class also implements the ASE</span>
<span class="s2">:class:`ase.calculators.interface.Calculator` interface via the</span>
<span class="s2">the :meth:`get_forces`, :meth:`get_stress`, :meth:`get_stresses`,</span>
<span class="s2">:meth:`get_potential_energy`, :meth:`get_potential_energies`</span>
<span class="s2">methods. For example::</span>

<span class="s2">    atoms = diamond(5.44, 14)</span>
<span class="s2">    atoms.rattle(0.01)</span>
<span class="s2">    atoms.set_calculator(pot)</span>
<span class="s2">    forces = atoms.get_forces()</span>
<span class="s2">    print forces</span>

<span class="s2">Note that the ASE force array is the transpose of the QUIP force</span>
<span class="s2">array, so has shape (len(atoms), 3) rather than (3, len(atoms)).</span>

<span class="s2">The optional arguments `pot1`, `pot2` and `bulk_scale` are</span>
<span class="s2">used by ``Sum`` and ``ForceMixing`` potentials (see also</span>
<span class="s2">wrapper class :class:`ForceMixingPotential`)</span>

<span class="s2">An :class:`quippy.mpi_context.MPI_context` object can be</span>
<span class="s2">passed as the `mpi_obj` argument to restrict the</span>
<span class="s2">parallelisation of this potential to a subset of the</span>

<span class="s2">The `callback` argument is used to implement the calculation of</span>
<span class="s2">the :class:`Potential` in a Python function: see :meth:`set_callback` for</span>
<span class="s2">an example.</span>

<span class="s2">In addition to the builtin QUIP potentials, it is possible to</span>
<span class="s2">use any ASE calculator as a QUIP potential by passing it as</span>
<span class="s2">the `calculator` argument to the :class:`Potential` constructor, e.g.::</span>

<span class="s2">   from ase.calculators.morse import MorsePotential</span>
<span class="s2">   pot = Potential(calculator=MorsePotential)</span>

<span class="s2">`atoms` if given, is used to set the calculator associated</span>
<span class="s2">with `atoms` to the new :class:`Potential` instance, by calling</span>
<span class="s2">:meth:&#39;.Atoms.set_calculator`.</span>

<span class="s2">.. note::</span>

<span class="s2">    QUIP potentials do not compute stress and per-atom stresses</span>
<span class="s2">    directly, but rather the virial tensor which has units of stress</span>
<span class="s2">    :math:`\times` volume, i.e. energy. If the total stress is</span>
<span class="s2">    requested, it is computed by dividing the virial by the atomic</span>
<span class="s2">    volume, obtained by calling :meth:`.Atoms.get_volume`. If per-atom</span>
<span class="s2">    stresses are requested, a per-atom volume is needed. By default</span>
<span class="s2">    this is taken to be the total volume divided by the number of</span>
<span class="s2">    atoms. In some cases, e.g. for systems containing large amounts of</span>
<span class="s2">    vacuum, this is not reasonable. The ``vol_per_atom`` calc_arg can</span>
<span class="s2">    be used either to give a single per-atom volume, or the name of an</span>
<span class="s2">    array in :attr:`.Atoms.arrays` containing volumes for each atom.</span>

<span class="s2">&quot;&quot;&quot;</span><span class="p">,</span>
    <span class="n">signature</span><span class="o">=</span><span class="s1">&#39;Potential(init_args[, pot1, pot2, param_str, param_filename, bulk_scale, mpi_obj, callback, calculator, atoms, calculation_always_required])&#39;</span><span class="p">)</span>

    <span class="n">callback_map</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="n">implemented_properties</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">,</span> <span class="s1">&#39;energies&#39;</span><span class="p">,</span> <span class="s1">&#39;forces&#39;</span><span class="p">,</span> <span class="s1">&#39;stress&#39;</span><span class="p">,</span> <span class="s1">&#39;stresses&#39;</span><span class="p">,</span>
                              <span class="s1">&#39;numeric_forces&#39;</span><span class="p">,</span> <span class="s1">&#39;elastic_constants&#39;</span><span class="p">,</span>
                              <span class="s1">&#39;unrelaxed_elastic_constants&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">init_args</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">pot1</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">pot2</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">param_str</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">param_filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">bulk_scale</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">mpi_obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">calculator</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">atoms</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">calculation_always_required</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">fpointer</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">finalise</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                 <span class="n">error</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_calc_args</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_default_properties</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">calculation_always_required</span> <span class="o">=</span> <span class="n">calculation_always_required</span>
        <span class="n">Calculator</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="o">=</span><span class="n">atoms</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">calculator</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">init_args</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">init_args</span> <span class="o">=</span> <span class="s1">&#39;callbackpot&#39;</span>

        <span class="n">param_dirname</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">param_filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">param_str</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">param_filename</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
            <span class="n">param_dirname</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">param_filename</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">init_args</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">param_str</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Need one of init_args,param_str,param_filename&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">init_args</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">init_args</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;callbackpot&#39;</span><span class="p">):</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="s1">&#39;label&#39;</span> <span class="ow">in</span> <span class="n">init_args</span><span class="p">:</span>
                    <span class="n">init_args</span> <span class="o">=</span> <span class="n">init_args</span> <span class="o">+</span> <span class="s1">&#39; label=</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># if param_str missing, try to find default set of QUIP params,</span>
                <span class="c1"># falling back on a do-nothing parameter string.</span>
                <span class="k">if</span> <span class="n">param_str</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">pot1</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">pot2</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">param_str</span> <span class="o">=</span> <span class="n">quip_xml_parameters</span><span class="p">(</span><span class="n">init_args</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
                        <span class="n">param_str</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">&#39;&lt;params&gt;&lt;/params&gt;&#39;</span>

        <span class="k">if</span> <span class="n">kwargs</span> <span class="o">!=</span> <span class="p">{}:</span>
            <span class="k">if</span> <span class="n">init_args</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">init_args</span> <span class="o">=</span> <span class="n">init_args</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="n">dict_to_args_str</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">init_args</span> <span class="o">=</span> <span class="n">dict_to_args_str</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c1"># Change to the xml directory to initialise, so that extra files</span>
        <span class="c1"># like sparseX can be found.</span>
        <span class="n">old_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">param_dirname</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">param_dirname</span><span class="p">)</span>

            <span class="n">_potential</span><span class="o">.</span><span class="n">Potential</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">init_args</span><span class="p">,</span>
                                          <span class="n">pot1</span><span class="o">=</span><span class="n">pot1</span><span class="p">,</span> <span class="n">pot2</span><span class="o">=</span><span class="n">pot2</span><span class="p">,</span>
                                          <span class="n">param_str</span><span class="o">=</span><span class="n">param_str</span><span class="p">,</span>
                                          <span class="n">bulk_scale</span><span class="o">=</span><span class="n">bulk_scale</span><span class="p">,</span>
                                          <span class="n">mpi_obj</span><span class="o">=</span><span class="n">mpi_obj</span><span class="p">,</span>
                                          <span class="n">fpointer</span><span class="o">=</span><span class="n">fpointer</span><span class="p">,</span> <span class="n">finalise</span><span class="o">=</span><span class="n">finalise</span><span class="p">,</span>
                                          <span class="n">error</span><span class="o">=</span><span class="n">error</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">old_dir</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">init_args</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">init_args</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;callbackpot&#39;</span><span class="p">):</span>
            <span class="n">_potential</span><span class="o">.</span><span class="n">Potential</span><span class="o">.</span><span class="n">set_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">Potential</span><span class="o">.</span><span class="n">callback</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">set_callback</span><span class="p">(</span><span class="n">callback</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">calculator</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">set_callback</span><span class="p">(</span><span class="n">calculator_callback_factory</span><span class="p">(</span><span class="n">calculator</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">atoms</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">atoms</span><span class="o">.</span><span class="n">set_calculator</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">init_args</span>

    <span class="fm">__init__</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">_potential</span><span class="o">.</span><span class="n">Potential</span><span class="o">.</span><span class="fm">__init__</span><span class="o">.</span><span class="vm">__doc__</span>


<div class="viewcode-block" id="Potential.calc"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.calc">[docs]</a>    <span class="k">def</span> <span class="nf">calc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">at</span><span class="p">,</span> <span class="n">energy</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">virial</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
             <span class="n">local_energy</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">local_virial</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
             <span class="n">args_str</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">error</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">args_str</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>
           <span class="n">args_str</span> <span class="o">=</span> <span class="n">dict_to_args_str</span><span class="p">(</span><span class="n">args_str</span><span class="p">)</span>

        <span class="n">kw_args_str</span> <span class="o">=</span> <span class="n">dict_to_args_str</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="n">args_str</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">get_calc_args_str</span><span class="p">(),</span> <span class="n">kw_args_str</span><span class="p">,</span> <span class="n">args_str</span><span class="p">))</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">energy</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; energy=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">energy</span>
            <span class="n">energy</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">energy</span><span class="p">,</span> <span class="nb">bool</span><span class="p">)</span> <span class="ow">and</span> <span class="n">energy</span><span class="p">:</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; energy&#39;</span>
            <span class="n">energy</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">force</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; force=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">force</span>
            <span class="n">force</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">force</span><span class="p">,</span> <span class="nb">bool</span><span class="p">)</span> <span class="ow">and</span> <span class="n">force</span><span class="p">:</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; force&#39;</span>
            <span class="n">force</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">virial</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; virial=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">virial</span>
            <span class="n">virial</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">virial</span><span class="p">,</span> <span class="nb">bool</span><span class="p">)</span> <span class="ow">and</span> <span class="n">virial</span><span class="p">:</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; virial&#39;</span>
            <span class="n">virial</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">local_energy</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; local_energy=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">local_energy</span>
            <span class="n">local_energy</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">local_energy</span><span class="p">,</span> <span class="nb">bool</span><span class="p">)</span> <span class="ow">and</span> <span class="n">local_energy</span><span class="p">:</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; local_energy&#39;</span>
            <span class="n">local_energy</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">local_virial</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; local_virial=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">local_virial</span>
            <span class="n">local_virial</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">local_virial</span><span class="p">,</span> <span class="nb">bool</span><span class="p">)</span> <span class="ow">and</span> <span class="n">local_virial</span><span class="p">:</span>
            <span class="n">args_str</span> <span class="o">=</span> <span class="n">args_str</span> <span class="o">+</span> <span class="s1">&#39; local_virial&#39;</span>
            <span class="n">local_virial</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="n">potlog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Potential invoking calc() on n=</span><span class="si">%d</span><span class="s1"> atoms with args_str &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span>
                     <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">at</span><span class="p">),</span> <span class="n">args_str</span><span class="p">))</span>
        <span class="n">_potential</span><span class="o">.</span><span class="n">Potential</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">at</span><span class="p">,</span> <span class="n">energy</span><span class="p">,</span> <span class="n">force</span><span class="p">,</span>
                                  <span class="n">virial</span><span class="p">,</span> <span class="n">local_energy</span><span class="p">,</span>
                                  <span class="n">local_virial</span><span class="p">,</span>
                                  <span class="n">args_str</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span></div>

    <span class="n">calc</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">update_doc_string</span><span class="p">(</span><span class="n">_potential</span><span class="o">.</span><span class="n">Potential</span><span class="o">.</span><span class="n">calc</span><span class="o">.</span><span class="vm">__doc__</span><span class="p">,</span>
       <span class="sd">&quot;&quot;&quot;In Python, this method is overloaded to set the final args_str to</span>
<span class="sd">          :meth:`get_calc_args_str`, followed by any keyword arguments,</span>
<span class="sd">          followed by an explicit `args_str` argument if present. This ordering</span>
<span class="sd">          ensures arguments explicitly passed to :meth:`calc` will override any</span>
<span class="sd">          default arguments.&quot;&quot;&quot;</span><span class="p">)</span>


    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">callback</span><span class="p">(</span><span class="n">at_ptr</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">quippy</span> <span class="k">import</span> <span class="n">Atoms</span>
        <span class="n">at</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">fpointer</span><span class="o">=</span><span class="n">at_ptr</span><span class="p">,</span> <span class="n">finalise</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">if</span> <span class="s1">&#39;label&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span> <span class="ow">or</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;label&#39;</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">Potential</span><span class="o">.</span><span class="n">callback_map</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown Callback label </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;label&#39;</span><span class="p">])</span>
        <span class="n">Potential</span><span class="o">.</span><span class="n">callback_map</span><span class="p">[</span><span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;label&#39;</span><span class="p">]](</span><span class="n">at</span><span class="p">)</span>


<div class="viewcode-block" id="Potential.set_callback"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.set_callback">[docs]</a>    <span class="k">def</span> <span class="nf">set_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">callback</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        For a :class:`Potential` of type `CallbackPot`, this method is</span>
<span class="sd">        used to set the callback function. `callback` should be a Python</span>
<span class="sd">        function (or other callable, such as a bound method or class</span>
<span class="sd">        instance) which takes a single argument, of type</span>
<span class="sd">        :class:`~quippy.atoms.Atoms`. Information about which properties should be</span>
<span class="sd">        computed can be obtained from the `calc_energy`, `calc_local_e`,</span>
<span class="sd">        `calc_force`, and `calc_virial` keys in `at.params`. Results</span>
<span class="sd">        should be returned either as `at.params` entries (for energy and</span>
<span class="sd">        virial) or by adding new atomic properties (for forces and local</span>
<span class="sd">        energy).</span>

<span class="sd">        Here&#39;s an example implementation of a simple callback::</span>

<span class="sd">          def example_callback(at):</span>
<span class="sd">              if at.calc_energy:</span>
<span class="sd">                 at.params[&#39;energy&#39;] = ...</span>

<span class="sd">              if at.calc_force:</span>
<span class="sd">                 at.add_property(&#39;force&#39;, 0.0, n_cols=3)</span>
<span class="sd">                 at.force[:,:] = ...</span>

<span class="sd">          p = Potential(&#39;CallbackPot&#39;)</span>
<span class="sd">          p.set_callback(example_callback)</span>
<span class="sd">          p.calc(at, energy=True)</span>
<span class="sd">          print at.energy</span>
<span class="sd">          ...</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">Potential</span><span class="o">.</span><span class="n">callback_map</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">))]</span> <span class="o">=</span> <span class="n">callback</span></div>


<div class="viewcode-block" id="Potential.check_state"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.check_state">[docs]</a>    <span class="k">def</span> <span class="nf">check_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-15</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">calculation_always_required</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">all_changes</span>
        <span class="k">return</span> <span class="n">Calculator</span><span class="o">.</span><span class="n">check_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">,</span> <span class="n">tol</span><span class="p">)</span></div>


<div class="viewcode-block" id="Potential.calculate"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.calculate">[docs]</a>    <span class="k">def</span> <span class="nf">calculate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">,</span> <span class="n">properties</span><span class="p">,</span> <span class="n">system_changes</span><span class="p">):</span>
        <span class="n">Calculator</span><span class="o">.</span><span class="n">calculate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">,</span> <span class="n">properties</span><span class="p">,</span> <span class="n">system_changes</span><span class="p">)</span>

        <span class="c1"># we will do the calculation in place, to minimise number of copies,</span>
        <span class="c1"># unless atoms is not a quippy Atoms</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">atoms</span><span class="p">,</span> <span class="n">Atoms</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">proxy</span><span class="p">(</span><span class="n">atoms</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">potlog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Potential atoms is not quippy.Atoms instance, copy forced!&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">atoms</span><span class="p">)</span>
        <span class="n">initial_arrays</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">arrays</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
        <span class="n">initial_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">info</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">properties</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">properties</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">,</span> <span class="s1">&#39;forces&#39;</span><span class="p">,</span> <span class="s1">&#39;stress&#39;</span><span class="p">]</span>

        <span class="c1"># Add any default properties</span>
        <span class="n">properties</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_default_properties</span><span class="p">()</span> <span class="o">+</span> <span class="n">properties</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">properties</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Nothing to calculate&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">calculation_required</span><span class="p">(</span><span class="n">atoms</span><span class="p">,</span> <span class="n">properties</span><span class="p">):</span>
            <span class="k">return</span>

        <span class="n">args_map</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;energy&#39;</span><span class="p">:</span>          <span class="p">{</span><span class="s1">&#39;energy&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">},</span>
            <span class="s1">&#39;energies&#39;</span><span class="p">:</span>        <span class="p">{</span><span class="s1">&#39;local_energy&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">},</span>
            <span class="s1">&#39;forces&#39;</span><span class="p">:</span>          <span class="p">{</span><span class="s1">&#39;force&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">},</span>
            <span class="s1">&#39;stress&#39;</span><span class="p">:</span>          <span class="p">{</span><span class="s1">&#39;virial&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">},</span>
            <span class="s1">&#39;numeric_forces&#39;</span><span class="p">:</span>  <span class="p">{</span><span class="s1">&#39;force&#39;</span><span class="p">:</span> <span class="s1">&#39;numeric_force&#39;</span><span class="p">,</span>
                                <span class="s1">&#39;force_using_fd&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
                                <span class="s1">&#39;force_fd_delta&#39;</span><span class="p">:</span> <span class="mf">1.0e-5</span><span class="p">},</span>
            <span class="s1">&#39;stresses&#39;</span><span class="p">:</span>        <span class="p">{</span><span class="s1">&#39;local_virial&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">},</span>
            <span class="s1">&#39;elastic_constants&#39;</span><span class="p">:</span> <span class="p">{},</span>
            <span class="s1">&#39;unrelaxed_elastic_constants&#39;</span><span class="p">:</span> <span class="p">{}</span>
            <span class="p">}</span>

        <span class="c1"># list of properties that require a call to Potential.calc()</span>
        <span class="n">calc_properties</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">,</span> <span class="s1">&#39;energies&#39;</span><span class="p">,</span> <span class="s1">&#39;forces&#39;</span><span class="p">,</span> <span class="s1">&#39;numeric_forces&#39;</span><span class="p">,</span> <span class="s1">&#39;stress&#39;</span><span class="p">,</span> <span class="s1">&#39;stresses&#39;</span><span class="p">]</span>

        <span class="c1"># list of other properties we know how to calculate</span>
        <span class="n">other_properties</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;elastic_constants&#39;</span><span class="p">,</span> <span class="s1">&#39;unrelaxed_elastic_constants&#39;</span><span class="p">]</span>

        <span class="n">calc_args</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">calc_required</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="nb">property</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">property</span> <span class="ow">in</span> <span class="n">calc_properties</span><span class="p">:</span>
                <span class="n">calc_required</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">calc_args</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">args_map</span><span class="p">[</span><span class="nb">property</span><span class="p">])</span>
            <span class="k">elif</span> <span class="nb">property</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">other_properties</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Don&#39;t know how to calculate property &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="nb">property</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">calc_required</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="p">,</span> <span class="n">args_str</span><span class="o">=</span><span class="n">dict_to_args_str</span><span class="p">(</span><span class="n">calc_args</span><span class="p">))</span>

        <span class="k">if</span> <span class="s1">&#39;energy&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">energy</span><span class="p">)</span>
        <span class="k">if</span> <span class="s1">&#39;energies&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;energies&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">local_energy</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span>
        <span class="k">if</span> <span class="s1">&#39;forces&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;forces&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">force</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
        <span class="k">if</span> <span class="s1">&#39;numeric_forces&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;numeric_forces&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">numeric_force</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
        <span class="k">if</span> <span class="s1">&#39;stress&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="n">stress</span> <span class="o">=</span> <span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">virial</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span><span class="o">/</span><span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">get_volume</span><span class="p">()</span>
            <span class="c1"># convert to 6-element array in Voigt order</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;stress&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">stress</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">stress</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">stress</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
                                               <span class="n">stress</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">stress</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">stress</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
        <span class="k">if</span> <span class="s1">&#39;stresses&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="n">lv</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">local_virial</span><span class="p">)</span> <span class="c1"># make a copy</span>
            <span class="n">vol_per_atom</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;vol_per_atom&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">get_volume</span><span class="p">()</span><span class="o">/</span><span class="nb">len</span><span class="p">(</span><span class="n">atoms</span><span class="p">))</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">vol_per_atom</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>
                <span class="n">vol_per_atom</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">arrays</span><span class="p">[</span><span class="n">vol_per_atom</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;stresses&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">lv</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">atoms</span><span class="p">),</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">order</span><span class="o">=</span><span class="s1">&#39;F&#39;</span><span class="p">)</span><span class="o">/</span><span class="n">vol_per_atom</span>

        <span class="k">if</span> <span class="s1">&#39;elastic_constants&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="n">cij_dx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;cij_dx&#39;</span><span class="p">,</span> <span class="mf">1e-2</span><span class="p">)</span>
            <span class="n">cij</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">calc_elastic_constants</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="p">,</span> <span class="n">fd</span><span class="o">=</span><span class="n">cij_dx</span><span class="p">,</span>
                                        <span class="n">args_str</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">get_calc_args_str</span><span class="p">(),</span>
                                        <span class="n">c</span><span class="o">=</span><span class="n">cij</span><span class="p">,</span> <span class="n">relax_initial</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">return_relaxed</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">get_fortran_indexing</span><span class="p">():</span>
                <span class="n">cij</span> <span class="o">=</span> <span class="n">cij</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;elastic_constants&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cij</span>

        <span class="k">if</span> <span class="s1">&#39;unrelaxed_elastic_constants&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="n">cij_dx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;cij_dx&#39;</span><span class="p">,</span> <span class="mf">1e-2</span><span class="p">)</span>
            <span class="n">c0ij</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">calc_elastic_constants</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="p">,</span> <span class="n">fd</span><span class="o">=</span><span class="n">cij_dx</span><span class="p">,</span>
                                        <span class="n">args_str</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">get_calc_args_str</span><span class="p">(),</span>
                                        <span class="n">c0</span><span class="o">=</span><span class="n">c0ij</span><span class="p">,</span> <span class="n">relax_initial</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">return_relaxed</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">get_fortran_indexing</span><span class="p">():</span>
                <span class="n">c0ij</span> <span class="o">=</span> <span class="n">c0ij</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;unrelaxed_elastic_constants&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">c0ij</span>

        <span class="c1"># copy back any additional output data to results dictionary</span>
        <span class="n">skip_keys</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">,</span> <span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="s1">&#39;virial&#39;</span><span class="p">,</span> <span class="s1">&#39;numeric_force&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">arrays</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">initial_arrays</span> <span class="ow">and</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">skip_keys</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">arrays</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">info</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">initial_info</span> <span class="ow">and</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">skip_keys</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">info</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">info</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quippy_atoms</span><span class="o">.</span><span class="n">info</span><span class="p">[</span><span class="n">key</span><span class="p">]</span></div>


    <span class="k">def</span> <span class="nf">get_potential_energy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">,</span> <span class="n">force_consistent</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_property</span><span class="p">(</span><span class="s1">&#39;energy&#39;</span><span class="p">,</span> <span class="n">atoms</span><span class="p">)</span>

<div class="viewcode-block" id="Potential.get_potential_energies"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get_potential_energies">[docs]</a>    <span class="k">def</span> <span class="nf">get_potential_energies</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return array of atomic energies calculated with this Potential</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_property</span><span class="p">(</span><span class="s1">&#39;energies&#39;</span><span class="p">,</span> <span class="n">atoms</span><span class="p">)</span></div>


<div class="viewcode-block" id="Potential.get_numeric_forces"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get_numeric_forces">[docs]</a>    <span class="k">def</span> <span class="nf">get_numeric_forces</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return forces on `atoms` computed with finite differences of the energy</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_property</span><span class="p">(</span><span class="s1">&#39;numeric_forces&#39;</span><span class="p">,</span> <span class="n">atoms</span><span class="p">)</span></div>


<div class="viewcode-block" id="Potential.get_stresses"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get_stresses">[docs]</a>    <span class="k">def</span> <span class="nf">get_stresses</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the per-atoms virial stress tensors for `atoms` computed with this Potential</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_property</span><span class="p">(</span><span class="s1">&#39;stresses&#39;</span><span class="p">,</span> <span class="n">atoms</span><span class="p">)</span></div>


<div class="viewcode-block" id="Potential.get_elastic_constants"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get_elastic_constants">[docs]</a>    <span class="k">def</span> <span class="nf">get_elastic_constants</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate elastic constants of `atoms` using this Potential.</span>

<span class="sd">        Returns  6x6 matrix :math:`C_{ij}` of elastic constants.</span>

<span class="sd">        The elastic contants are calculated as finite difference</span>
<span class="sd">        derivatives of the virial stress tensor using positive and</span>
<span class="sd">        negative strains of magnitude the `cij_dx` entry in</span>
<span class="sd">        ``calc_args``.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_property</span><span class="p">(</span><span class="s1">&#39;elastic_constants&#39;</span><span class="p">,</span> <span class="n">atoms</span><span class="p">)</span></div>


<div class="viewcode-block" id="Potential.get_unrelaxed_elastic_constants"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get_unrelaxed_elastic_constants">[docs]</a>    <span class="k">def</span> <span class="nf">get_unrelaxed_elastic_constants</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate unrelaxed elastic constants of `atoms` using this Potential</span>

<span class="sd">        Returns 6x6 matrix :math:`C^0_{ij}` of unrelaxed elastic constants.</span>

<span class="sd">        The elastic contants are calculated as finite difference</span>
<span class="sd">        derivatives of the virial stress tensor using positive and</span>
<span class="sd">        negative strains of magnitude the `cij_dx` entry in</span>
<span class="sd">        :attr:`calc_args`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_property</span><span class="p">(</span><span class="s1">&#39;unrelaxed_elastic_constants&#39;</span><span class="p">,</span> <span class="n">atoms</span><span class="p">)</span></div>


<div class="viewcode-block" id="Potential.get_default_properties"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get_default_properties">[docs]</a>    <span class="k">def</span> <span class="nf">get_default_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Get the list of properties to be calculated by default&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_default_properties</span><span class="p">[:]</span></div>


<div class="viewcode-block" id="Potential.set_default_properties"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.set_default_properties">[docs]</a>    <span class="k">def</span> <span class="nf">set_default_properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">properties</span><span class="p">):</span>
        <span class="s2">&quot;Set the list of properties to be calculated by default&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_default_properties</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[:]</span></div>


<div class="viewcode-block" id="Potential.get"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get">[docs]</a>    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the value of a ``calc_args`` parameter for this :class:`Potential`</span>

<span class="sd">        Returns ``None`` if `param` is not in the current ``calc_args`` dictionary.</span>

<span class="sd">        All calc_args are passed to :meth:`calc` whenever energies,</span>
<span class="sd">        forces or stresses need to be re-computed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calc_args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">param</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span></div>


<div class="viewcode-block" id="Potential.set"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.set">[docs]</a>    <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set one or more calc_args parameters for this Potential</span>

<span class="sd">        All calc_args are passed to :meth:`calc` whenever energies,</span>
<span class="sd">        forces or stresses need to be computed.</span>

<span class="sd">        After updating the calc_args, :meth:`set` calls :meth:`reset`</span>
<span class="sd">        to mark all properties as needing to be recaculated.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_calc_args</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span></div>


<div class="viewcode-block" id="Potential.get_calc_args"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get_calc_args">[docs]</a>    <span class="k">def</span> <span class="nf">get_calc_args</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the current ``calc_args``</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calc_args</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span></div>


<div class="viewcode-block" id="Potential.set_calc_args"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.set_calc_args">[docs]</a>    <span class="k">def</span> <span class="nf">set_calc_args</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">calc_args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set the ``calc_args`` to be used subsequent :meth:`calc` calls</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_calc_args</span> <span class="o">=</span> <span class="n">calc_args</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span></div>


<div class="viewcode-block" id="Potential.get_calc_args_str"><a class="viewcode-back" href="../../potential.html#quippy.potential.Potential.get_calc_args_str">[docs]</a>    <span class="k">def</span> <span class="nf">get_calc_args_str</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the ``calc_args`` to be passed to :meth:`calc` as a string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dict_to_args_str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_calc_args</span><span class="p">)</span></div></div>


<span class="kn">from</span> <span class="nn">quippy</span> <span class="k">import</span> <span class="n">FortranDerivedTypes</span>
<span class="n">FortranDerivedTypes</span><span class="p">[</span><span class="s1">&#39;type(potential)&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Potential</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">ase.optimize.optimize</span> <span class="k">import</span> <span class="n">Optimizer</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="n">Optimizer</span> <span class="o">=</span> <span class="nb">object</span>

<div class="viewcode-block" id="Minim"><a class="viewcode-back" href="../../potential.html#quippy.potential.Minim">[docs]</a><span class="k">class</span> <span class="nc">Minim</span><span class="p">(</span><span class="n">Optimizer</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Minimise forces and/or virial tensor components wrt atomic</span>
<span class="sd">    positions and/or cell.</span>

<span class="sd">    This class is a wrapper around the</span>
<span class="sd">    :meth:`quippy.potential.Potential.minim` routine, compatible with</span>
<span class="sd">    the ASE `optimizer inferface &lt;https://wiki.fysik.dtu.dk/ase/ase/optimize.html&gt;`_.</span>

<span class="sd">    `method` should be one of ``&quot;sd&quot;``, (steepest descent), ``&quot;cg&quot;``</span>
<span class="sd">    (conjugate gradients, the default), ``&quot;cg_n&quot;`` (Noam Bernstein&#39;s conjugate gradients</span>
<span class="sd">    implementation), ``&quot;pcg&quot;``, (preconditioned conjugate gradients),</span>
<span class="sd">    ``&quot;lbfgs&quot;`` (L-BFGS), or ``&quot;fire&quot;`` (FIRE).</span>

<span class="sd">    Example usage::</span>

<span class="sd">       from quippy.structures import diamond</span>
<span class="sd">       from quippy.potential import Potential, Minim</span>

<span class="sd">       orig_atoms = diamond(5.44, 14)</span>
<span class="sd">       atoms = orig_atoms.copy()</span>
<span class="sd">       atoms.rattle(0.01)  # randomise the atomic positions a little</span>

<span class="sd">       potential = Potential(&#39;IP SW&#39;)</span>
<span class="sd">       atoms.set_calculator(potential)</span>

<span class="sd">       minimiser = Minim(atoms, relax_positions=True, relax_cell=False)</span>
<span class="sd">       minimiser.run(fmax=0.01)</span>

<span class="sd">       print orig_atoms.positions - atoms.positions # should be approx zero</span>

<span class="sd">    Note that if the :class:`~quippy.atoms.Atoms` object passed to the</span>
<span class="sd">    :class:`Minim` constructor is a :class:`quippy.atoms.Atoms`</span>
<span class="sd">    instance, the minimisation is done in place.  Otherwise, a copy is</span>
<span class="sd">    made, but the relaxed positions and cell vectors are copied back</span>
<span class="sd">    at the end of the :meth`run` method.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">,</span> <span class="n">restart</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">relax_positions</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">relax_cell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                 <span class="n">logfile</span><span class="o">=</span><span class="s1">&#39;-&#39;</span><span class="p">,</span> <span class="n">trajectory</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">method</span><span class="o">=</span><span class="s1">&#39;cg&#39;</span><span class="p">,</span> <span class="n">linminroutine</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">eps_guess</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">fire_dt0</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">fire_dt_max</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">external_pressure</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">use_precond</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="n">calc</span> <span class="o">=</span> <span class="n">atoms</span><span class="o">.</span><span class="n">get_calculator</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">calc</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Atoms object has no calculator&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">calc</span><span class="p">,</span> <span class="n">Potential</span><span class="p">):</span>
            <span class="n">calc</span> <span class="o">=</span> <span class="n">Potential</span><span class="p">(</span><span class="n">calculator</span><span class="o">=</span><span class="n">calc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">potential</span> <span class="o">=</span> <span class="n">calc</span>

        <span class="c1"># we will do the calculation in place, to minimise number of copies,</span>
        <span class="c1"># unless atoms is not a quippy Atoms</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_atoms</span> <span class="o">=</span> <span class="n">atoms</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">atoms</span><span class="p">,</span> <span class="n">Atoms</span><span class="p">):</span>
            <span class="n">potlog</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;Minim atoms is not quippy.Atoms instance, copy forced!&#39;</span><span class="p">)</span>
            <span class="n">atoms</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">atoms</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span> <span class="o">=</span> <span class="n">atoms</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">nsteps</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="n">restart</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span>
        <span class="k">if</span> <span class="n">logfile</span> <span class="o">!=</span> <span class="s1">&#39;-&#39;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span>
        <span class="k">if</span> <span class="n">trajectory</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">method</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">linminroutine</span> <span class="o">=</span> <span class="n">linminroutine</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">do_pos</span> <span class="o">=</span> <span class="n">relax_positions</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">do_lat</span> <span class="o">=</span> <span class="n">relax_cell</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eps_guess</span> <span class="o">=</span> <span class="n">eps_guess</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fire_dt0</span> <span class="o">=</span> <span class="n">fire_dt0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fire_dt_max</span> <span class="o">=</span> <span class="n">fire_dt_max</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">external_pressure</span> <span class="o">=</span> <span class="n">external_pressure</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_precond</span> <span class="o">=</span> <span class="n">use_precond</span>


<div class="viewcode-block" id="Minim.run"><a class="viewcode-back" href="../../potential.html#quippy.potential.Minim.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fmax</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">steps</span><span class="o">=</span><span class="mi">100000000</span><span class="p">,</span> <span class="n">convergence_tol</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Run the minimiser until maximum force is below `fmax`.</span>

<span class="sd">        Maximum number of minimisation steps is given by `steps`.</span>

<span class="sd">        Note that QUIP minim convergence criteria is actually based on</span>
<span class="sd">        :math:`|\mathbf{f}|^2` rather than on the maximum force. Here</span>
<span class="sd">        we convert using the relation::</span>

<span class="sd">                convergence_tol = 3*len(atoms)*fmax**2</span>

<span class="sd">        which is only approximately equivalent. To specify the converengence</span>
<span class="sd">        tolerance exactly, pass a value for the `convergence_tol` argument.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">convergence_tol</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">convergence_tol</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="p">)</span><span class="o">*</span><span class="n">fmax</span><span class="o">**</span><span class="mi">2</span> <span class="c1"># FIXME only approximately equivalent</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_atoms</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">do_pos</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">set_positions</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_atoms</span><span class="o">.</span><span class="n">get_positions</span><span class="p">())</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">do_lat</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">set_cell</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_atoms</span><span class="o">.</span><span class="n">get_cell</span><span class="p">())</span>

        <span class="c1"># check for constraints, only FixAtoms is supported for now</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">constraints</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="kn">import</span> <span class="nn">ase.constraints</span>
            <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;atoms has constraints but cannot import ase.constraints module&#39;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">constraint</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">constraints</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">constraint</span><span class="p">,</span> <span class="n">ase</span><span class="o">.</span><span class="n">constraints</span><span class="o">.</span><span class="n">FixAtoms</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;move_mask&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">move_mask</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)[</span><span class="n">constraint</span><span class="o">.</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 0-based indices</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;cannot convert ASE constraint </span><span class="si">%r</span><span class="s1"> to QUIP constraint&#39;</span> <span class="o">%</span> <span class="n">constraint</span><span class="p">)</span>

        <span class="n">potlog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Minim.run() calling QUIP minim() with convergence_tol </span><span class="si">%f</span><span class="s1"> and args_str </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                     <span class="p">(</span><span class="n">convergence_tol</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">potential</span><span class="o">.</span><span class="n">get_calc_args_str</span><span class="p">()))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nsteps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">potential</span><span class="o">.</span><span class="n">minim</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="p">,</span> <span class="n">convergence_tol</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span>
                                           <span class="bp">self</span><span class="o">.</span><span class="n">linminroutine</span><span class="p">,</span> <span class="n">do_pos</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">do_pos</span><span class="p">,</span> <span class="n">do_lat</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">do_lat</span><span class="p">,</span>
                                           <span class="n">args_str</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">potential</span><span class="o">.</span><span class="n">get_calc_args_str</span><span class="p">(),</span> <span class="n">eps_guess</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">eps_guess</span><span class="p">,</span>
                                           <span class="n">fire_minim_dt0</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fire_dt0</span><span class="p">,</span> <span class="n">fire_minim_dt_max</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fire_dt_max</span><span class="p">,</span>
                                           <span class="n">external_pressure</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">external_pressure</span><span class="p">,</span>
                                           <span class="n">use_precond</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">use_precond</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">atoms</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_atoms</span><span class="p">:</span>
            <span class="c1"># update with results of minimisation</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">do_pos</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_atoms</span><span class="o">.</span><span class="n">set_positions</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">get_positions</span><span class="p">())</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">do_lat</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_atoms</span><span class="o">.</span><span class="n">set_cell</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">get_cell</span><span class="p">())</span></div>



<div class="viewcode-block" id="Minim.get_number_of_steps"><a class="viewcode-back" href="../../potential.html#quippy.potential.Minim.get_number_of_steps">[docs]</a>    <span class="k">def</span> <span class="nf">get_number_of_steps</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return number of steps taken during minimisation</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">nsteps</span></div></div>


<div class="viewcode-block" id="ForceMixingPotential"><a class="viewcode-back" href="../../potential.html#quippy.potential.ForceMixingPotential">[docs]</a><span class="k">class</span> <span class="nc">ForceMixingPotential</span><span class="p">(</span><span class="n">Potential</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Subclass of :class:`Potential` for mixing forces from two Potentials</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pot1</span><span class="p">,</span> <span class="n">pot2</span><span class="p">,</span> <span class="n">bulk_scale</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">mpi_obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">calculator</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">atoms</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">qm_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">fpointer</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">finalise</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">error</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>

        <span class="n">args_str</span> <span class="o">=</span> <span class="s1">&#39;ForceMixing&#39;</span>
        <span class="n">Potential</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args_str</span><span class="p">,</span>
                           <span class="n">pot1</span><span class="o">=</span><span class="n">pot1</span><span class="p">,</span> <span class="n">pot2</span><span class="o">=</span><span class="n">pot2</span><span class="p">,</span> <span class="n">bulk_scale</span><span class="o">=</span><span class="n">bulk_scale</span><span class="p">,</span>
                           <span class="n">mpi_obj</span><span class="o">=</span><span class="n">mpi_obj</span><span class="p">,</span>
                           <span class="n">atoms</span><span class="o">=</span><span class="n">atoms</span><span class="p">,</span> <span class="n">fpointer</span><span class="o">=</span><span class="n">fpointer</span><span class="p">,</span> <span class="n">finalise</span><span class="o">=</span><span class="n">finalise</span><span class="p">,</span>
                           <span class="n">error</span><span class="o">=</span><span class="n">error</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">qm_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">set_qm_atoms</span><span class="p">(</span><span class="n">qm_list</span><span class="p">,</span> <span class="n">atoms</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>


<div class="viewcode-block" id="ForceMixingPotential.get_qm_atoms"><a class="viewcode-back" href="../../potential.html#quippy.potential.ForceMixingPotential.get_qm_atoms">[docs]</a>    <span class="k">def</span> <span class="nf">get_qm_atoms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">atoms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the current list of QM atom indices as a list</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">atoms</span><span class="o">.</span><span class="n">hybrid</span> <span class="o">==</span> <span class="n">HYBRID_ACTIVE_MARK</span><span class="p">)</span><span class="o">.</span><span class="n">nonzero</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span></div>


<div class="viewcode-block" id="ForceMixingPotential.set_qm_atoms"><a class="viewcode-back" href="../../potential.html#quippy.potential.ForceMixingPotential.set_qm_atoms">[docs]</a>    <span class="k">def</span> <span class="nf">set_qm_atoms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">qm_list</span><span class="p">,</span> <span class="n">atoms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set the QM atoms, given as a list of atom indices</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">atoms</span><span class="o">.</span><span class="n">has_property</span><span class="p">(</span><span class="s1">&#39;hybrid&#39;</span><span class="p">):</span>
            <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;hybrid&#39;</span><span class="p">,</span> <span class="n">HYBRID_NO_MARK</span><span class="p">)</span>
        <span class="n">atoms</span><span class="o">.</span><span class="n">hybrid</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">HYBRID_NO_MARK</span>
        <span class="n">atoms</span><span class="o">.</span><span class="n">hybrid</span><span class="p">[</span><span class="n">qm_list</span><span class="p">]</span> <span class="o">=</span> <span class="n">HYBRID_ACTIVE_MARK</span></div></div>


<div class="viewcode-block" id="force_test"><a class="viewcode-back" href="../../potential.html#quippy.potential.force_test">[docs]</a><span class="k">def</span> <span class="nf">force_test</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">dx</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compare analyric and numeric forces for the Potential `p` with Atoms `at`</span>

<span class="sd">    Finite difference derivates are calculated by moving each atom by `dx`.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">analytic_f</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">))</span>
    <span class="n">p</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="n">analytic_f</span><span class="p">)</span>
    <span class="n">num_f</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">))</span>
    <span class="n">ep</span><span class="p">,</span> <span class="n">em</span> <span class="o">=</span> <span class="n">farray</span><span class="p">(</span><span class="mf">0.0</span><span class="p">),</span> <span class="n">farray</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">):</span>
            <span class="n">ap</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="n">ap</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="n">j</span><span class="p">,</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">dx</span>
            <span class="n">p</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="n">energy</span><span class="o">=</span><span class="n">ep</span><span class="p">)</span>
            <span class="nb">print</span> <span class="s1">&#39;e+&#39;</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span><span class="n">i</span><span class="p">,</span><span class="n">ep</span>
            <span class="n">ap</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="n">j</span><span class="p">,</span><span class="n">i</span><span class="p">]</span> <span class="o">-=</span> <span class="mf">2.0</span><span class="o">*</span><span class="n">dx</span>
            <span class="n">p</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="n">energy</span><span class="o">=</span><span class="n">em</span><span class="p">)</span>
            <span class="nb">print</span> <span class="s1">&#39;e-&#39;</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span><span class="n">i</span><span class="p">,</span><span class="n">em</span>
            <span class="n">num_f</span><span class="p">[</span><span class="n">j</span><span class="p">,</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="p">(</span><span class="n">ep</span> <span class="o">-</span> <span class="n">em</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">dx</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">analytic_f</span><span class="p">,</span> <span class="n">num_f</span><span class="p">,</span> <span class="n">analytic_f</span> <span class="o">-</span> <span class="n">num_f</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2008-2016, James Kermode

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>